package com.yuedou.jylt.utils;


import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.content.FileProvider;

import com.xuexiang.xupdate.entity.DownloadEntity;
import com.xuexiang.xupdate.listener.OnInstallListener;

import java.io.File;

/**
 * 说明：
 *
 * @author Orange
 * @date 2021/10/20 15:48
 */
public class MyOnInstallListener implements OnInstallListener {
    @Override
    public boolean onInstallApk(@NonNull Context context, @NonNull File file, @NonNull DownloadEntity downloadEntity) {
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                Uri uri = FileProvider.getUriForFile(
                        context,
                        context.getPackageName() + ".provider",
                        file
                );
                intent.setDataAndType(uri, "application/vnd.android.package-archive");
            } else {
                intent.setDataAndType(
                        Uri.fromFile(file),
                        "application/vnd.android.package-archive"
                );
            }
            context.startActivity(intent);
        }catch (Exception ex){
            Log.e("TAG", "onInstallApk: ", ex);
            ex.printStackTrace();
        }
        return true;
    }

    @Override
    public void onInstallApkSuccess() {
        Log.d("TAG", "onInstallApkSuccess: 安装成功");
    }
}
